<template>
  <!-- 版心  -->
  <el-container class="main-container">
    <!-- 1.左侧菜单栏 -->
    <el-aside width="200px">
      <!-- 1.1 个人信息盒子 -->
      <div class="user-box">
        <img :src="user.user_pic" alt="" v-if="user.user_pic" />
        <img src="@/assets/images/images.jpg" v-else alt="" />
        <span>欢迎{{ user.nikename || user.username }}</span>
      </div>
      <!-- 1.2 菜单栏 -->
      <el-menu
        class="el-menu-vertical-demo"
        :default-active="$route.path"
        background-color="#23262e"
        text-color="#fff"
        active-text-color="#6cf"
        unique-opened
        router
      >
        <template v-for="(item) in menu" >
         <el-menu-item :index="item.indexPath" v-if="!item.children" :key="item.indexPath">
          <i :class="item.icon"></i>
          <span slot="title">{{item.title}}</span>
        </el-menu-item>
    
        <el-submenu :index="item.indexPath" v-else :key="item.indexPath">
          <template slot="title">
            <i :class="item.icon"></i>
            <span>{{item.title}}</span>
          </template>
          <el-menu-item-group>
            <template v-for="(item, index) in item.children" >
              
                <el-menu-item :index="item.indexPath" :key="index">
                   <i :class="item.icon" :key="index"></i>
                   {{item.title}}
                </el-menu-item>
            </template>
          </el-menu-item-group>
        </el-submenu>
        </template>
      </el-menu>
    </el-aside>
    <!-- 2.右侧内容 -->
    <el-container>
      <!-- 2.1 右侧顶部栏 -->
      <el-header>
        <!-- （1）左边一个logo -->
        <div class="logo"></div>
        <!-- （2）右边一个横着的menu -->
        <el-menu class="el-menu-vertical-demo" mode="horizontal" router>
          <el-submenu index="1">
            <template slot="title">
              <img
                :src="user.user_pic"
                alt=""
                v-if="user.user_pic"
               class="avatar"
              />
              <img
                src="@/assets/images/images.jpg"
                v-else
                alt=""
                class="avatar"
              />
              <span>个人中心</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="user-info"
                ><i class="el-icon-s-operation"></i>基本资料</el-menu-item
              >
              <el-menu-item index="user-avatar"
                ><i class="el-icon-camera"></i>修改头像</el-menu-item
              >
              <el-menu-item index="user-pwd"
                ><i class="el-icon-key"></i>修改密码</el-menu-item
              >
            </el-menu-item-group>
          </el-submenu>
          <el-menu-item >
            <span slot="title" @click="logout">
              <i class="el-icon-switch-button"></i>
              退出</span>
          </el-menu-item>
        </el-menu>
      </el-header>
      <!-- 2.2 右侧内容 -->
      <el-main>
        <!-- 二级路由出口 -->
        <router-view></router-view>
      </el-main>
      <!-- 2.3 右侧底部 -->
      <el-footer>© www.itheima.com - 黑马程序员</el-footer>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Main",
  data() {
    return {
      menu:[]
    };
  },
 async created() {
    //（1）发送ajax请求个人信息：提交actions
    this.$store.dispatch("getUserInfo");
    //（2）发送ajax请求左边菜单栏
    const {data:res} = await this.$axios.get('/my/menus')
         console.log(res);
         this.menu=res.data
  },
  //利用计算属性简写
  computed: {
    user() {
      return this.$store.state.user;
    },
  },
 methods: {
  async logout() {
 const confirm=await this.$confirm("您确认退出登录吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).catch(()=>{})
  if (confirm) {
    // 1. 清空 token
    this.$store.commit("updateToken", "");
    // 2. 跳转到登录页面
    this.$router.push("/login");
  }
}
 },
};
</script>

<style lang="less" scoped>
.main-container {
  height: 100%;
  .el-aside {
    background-color: #23262e;
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #f2f2f2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}

//右侧样式
.el-header {
  .logo {
    height: 45px;
    width: 150px;
    padding: 8px 0px;
    user-select: none;
    text-align: center;
    background: url("@/assets/images/logo.png") center center / 100px no-repeat
      rgb(85, 85, 85);
    border-radius: 3px;
    margin-left: 15px;
  }
}

// 左侧边栏用户信息区域
.user-box {
  height: 70px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-top: 1px solid #000;
  border-bottom: 1px solid #000;
  user-select: none;
  img {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    background-color: #fff;
    margin-right: 15px;
    object-fit: cover;
  }
  span {
    color: white;
    font-size: 12px;
  }
}

// 侧边栏菜单的样式
.el-aside {
  .el-submenu,
  .el-menu-item {
    width: 200px;
    user-select: none;
  }
}
</style>
